import Cookies from 'js-cookie'

/* 时间戳的键 */
const timeKey = 'timeStamp'
/* token的过期时间(1小时) */
const tokenSurvival = 3600

export function getTimeStamp() {
  return Cookies.get(timeKey)
}

export function setTimeStamp(now) {
  return Cookies.set(timeKey, now)
}

export function removeTimeStamp() {
  return Cookies.remove(timeKey)
}

/* 判断token是否过期的回调函数 */
export const tokenIsExpired = () => {
  const currentTimeStamp = Date.now()
  const formerlyTimeStamp = getTimeStamp() || 0
  return (currentTimeStamp - formerlyTimeStamp) / 1000 > tokenSurvival
}
